//
//  LuckyResultView.m
//  NewProject
//
//  Created by apple on 2018/6/2.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "LuckyResultView.h"

@implementation LuckyResultView{
    CallBackForResult nextAction;
    NSString* resultString;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (IBAction)playAgainAction:(id)sender {
    [self removeFromSuperview];
}

- (IBAction)nextAction:(id)sender {
    if (nextAction) {
        nextAction(resultString);
    }
}

- (IBAction)closeTapAction:(id)sender {

    [self removeFromSuperview];
}

-(void)loadNumberWith:(NSArray*)redArray and:(NSArray*)blueArray andCallBack:(CallBackForResult)callBack{
    for (UIView* view in self.numberBoxView.subviews) {
        [view removeFromSuperview];
    }
    CGFloat spacing = 5;
    CGFloat ballWidth = self.numberBoxView.frame.size.width/([redArray count]+[blueArray count])-spacing;
    if (ballWidth>28) {
        ballWidth=28;
    }
    CGFloat spacingX = 0;

    for (int i=0; i<[redArray count]; i++) {
        UIView* numberView = [[UIView alloc] initWithFrame:CGRectMake(i*(ballWidth+spacing), 0, ballWidth, ballWidth)];
        numberView.backgroundColor = [UIColor whiteColor];
        UILabel* numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ballWidth, ballWidth)];
        numberLabel.font = [UIFont boldSystemFontOfSize:12];
        numberLabel.textColor = kRGBColor(221,66,45);
        numberLabel.textAlignment = NSTextAlignmentCenter;
        numberLabel.text = [redArray objectAtIndex:i];
        [numberView addSubview:numberLabel];
        numberView.layer.cornerRadius = ballWidth*0.5;
        [self.numberBoxView addSubview:numberView];
        spacingX+=(ballWidth+spacing);
    }
    for (int i=0; i<[blueArray count]; i++) {
        UIView* numberView = [[UIView alloc] initWithFrame:CGRectMake(spacingX, 0, ballWidth, ballWidth)];
        numberView.backgroundColor = [UIColor whiteColor];
        UILabel* numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ballWidth, ballWidth)];
        numberLabel.font = [UIFont boldSystemFontOfSize:12];
        numberLabel.textColor = kRGBColor(66,151,255);
        numberLabel.textAlignment = NSTextAlignmentCenter;
        numberLabel.text = [blueArray objectAtIndex:i];
        [numberView addSubview:numberLabel];
        numberView.layer.cornerRadius = ballWidth*0.5;
        [self.numberBoxView addSubview:numberView];
        spacingX+=(ballWidth+spacing);
    }
    
    NSString* redString = [redArray componentsJoinedByString:@","];
    NSString* blueString = [blueArray componentsJoinedByString:@","];
    resultString = @"";
    resultString = redString;
    if ([blueString length]>0) {
        resultString = [NSString stringWithFormat:@"%@+%@",resultString,blueString];
    }
    
    if (callBack) {
        nextAction = callBack;
    }
    
}




@end
